package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 2/2/21 10:03 AM
 *
 * https://leetcode-cn.com/problems/longest-repeating-character-replacement/
 */
public class _424_替换后的最长重复字符 {

    public static int characterReplacement(String s, int k) {

        int[] counts = new int[26];
        int right = 0,left = 0;
        int maxIndex = -1;
        char[] chars = s.toCharArray();

        while (right < chars.length) {

            int index = chars[right] - 'A';
            counts[index] += 1;
            if (maxIndex < 0 || counts[maxIndex] < counts[index]) {
                maxIndex = index;
            }
//            [left,right]
            if (right - left + 1 - counts[maxIndex] > k) {

                counts[chars[left] - 'A']--;
                left++;
            }
            right++;

        }

        return right - left;
    }

    public static void main(String[] args) {

        System.out.println(characterReplacement("AABABBA", 1));
    }

}
